<template>
  <div>
    <el-form @submit.native.prevent :inline="true" class="demo-form-inline">
      <el-form-item label="教练名称">
        <el-input
          @keyup.native.enter="submit"
          v-model="names"
          placeholder="请输入关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData.result" style="width: 100%">
      <el-table-column fixed label="教练照片" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.coach_cover" width="100px" />
        </template>
      </el-table-column>
      <el-table-column prop="coach_name" label="教练名称"> </el-table-column>
      <el-table-column prop="coach_phone" label="联系方式"> </el-table-column>
      <el-table-column prop="subjectstype" label="驾照类型"> </el-table-column>
      <el-table-column prop="licensetype" label="教学科目"> </el-table-column>
      <el-table-column prop="coachlabel" label="教练标签"> </el-table-column>
      <el-table-column prop="schoolname" label="所属驾校"> </el-table-column>
      <el-table-column prop="city_name" label="所在城市"> </el-table-column>
      <el-table-column prop="coach_score" label="评分"> </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="$router.push('/home/coach-update/' + scope.row.coach_id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="det(scope.row)"
            >删除</el-button
          >
        </template> </el-table-column
      >>
    </el-table>
    <el-pagination
      style="margin: 10px"
      background
      :total="tableData.total"
      :page-size="tableData.pagesize"
      :current-page="tableData.page"
      layout="->, total, prev, pager, next, jumper"
      @current-change="changeCurrentPage"
    ></el-pagination>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      names: "",
      // 驾校列表数据
      tableData: {
        page: 1,
        pagesize: 10,
        total: 0,
        result: [],
      },
    };
  },
  methods: {
    submit() {
      this.tableData.page = 1;
      this.search();
    },

    det(coach) {
      this.$confirm("是否删除该教练信息", "提示", {
        ConfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        httpApi.coachApi.del({ coach_id: coach.coach_id }).then((res) => {
          console.log(this.coach_id);
          console.log("删除教练", res);
          this.search();
          this.$message.success("删除成功");
        });
      });
    },

    infoCoachlist() {
      let params = {
        page: this.tableData.page,
        pagesize: this.tableData.pagesize,
      };
      httpApi.coachApi.queryall(params).then((res) => {
        console.log("加载教练列表", res);
        this.tableData.result = res.data.data.result;
        this.tableData.total = res.data.data.total;
      });
    },

    queryCoachBuName() {
      let params = {
        page: this.tableData.page,
        pagesize: this.tableData.pagesize,
        name: this.names,
      };
      httpApi.coachApi.queryByNameLike(params).then((res) => {
        console.log("模糊查询教练", res);
        this.tableData.result = res.data.data.result;
        this.tableData.total = res.data.data.total;
      });
    },

    search() {
      if (this.names.trim()) {
        this.queryCoachBuName();
      } else {
        this.infoCoachlist();
      }
    },

    changeCurrentPage(page) {
      this.tableData.page = page; // 修改当前页
      this.search();
    },
  },
  mounted() {
    this.infoCoachlist();
  },
};
</script>

<style lang="scss" scoped></style>